﻿using System;
using System.Net;
using System.Windows.Forms;
using System.Web;

namespace BugClient
{
    public partial class EditForm : Form
    {
        WebClient web;
        string url;
        string token;
        string bugID;
        string title;
        string content;
        string priority;

        public EditForm(WebClient web, string url, string token, string bugID, string title, string content, string priority)
        {
            InitializeComponent();

            Text = "编辑-" + bugID;
            this.web = web;
            this.url = url;
            this.token = token;
            this.bugID = bugID;
            this.title = title;
            titleBox.Text = title;
            try { contentBox.Rtf = content; }
            catch { contentBox.Text = content;  }
            this.content = contentBox.Rtf;
            this.priority = priority;
            priorityBox.SelectedIndex = priorityBox.Items.IndexOf(priority);
        }

        private async void commitButton_Click(object sender, EventArgs e)
        {
            if (web.IsBusy)
                return;
            if (string.IsNullOrEmpty(titleBox.Text))
            {
                MessageBox.Show("标题不能为空");
                return;
            }
            if (titleBox.Text == title && contentBox.Rtf == content && string.IsNullOrEmpty(commentBox.Text)
                && (priorityBox.SelectedItem == null || priorityBox.SelectedItem.ToString() == priority))
            {
                MessageBox.Show("没有修改任何内容");
                return;
            }
            string response;
            try
            {
                string contentText;
                contentBox.SelectAll();
                if ((contentBox.SelectionType & RichTextBoxSelectionTypes.Object) == 0
                    && (contentBox.SelectionType & RichTextBoxSelectionTypes.MultiObject) == 0)
                    contentText = contentBox.Text;
                else
                    contentText = contentBox.Rtf;
                response = await web.UploadStringTaskAsync(url + "/edit",
                    string.Format("bug={0}&title={1}&content={2}&comment={3}&priority={4}&token={5}", bugID,
                    title == titleBox.Text ? "" : HttpUtility.UrlEncode(titleBox.Text),
                    content == contentBox.Rtf ? "" : HttpUtility.UrlEncode(contentText), HttpUtility.UrlEncode(commentBox.Text),
                    priorityBox.SelectedItem != null && priority == priorityBox.SelectedItem.ToString() ? "" : priorityBox.SelectedItem, token));
            }
            catch (WebException ex)
            {
                MessageBox.Show("连接服务器失败：" + ex.Message);
                return;
            }
            if (string.IsNullOrEmpty(response))
            {
                MessageBox.Show("编辑Bug失败");
                return;
            }
            DialogResult = DialogResult.OK;
            Dispose();
            Close();
        }
    }
}
